package com.jzhao.gper.demo.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jzhao.gper.demo.service.IModifyService;
import com.jzhao.gper.demo.service.IQueryService;
import com.jzhao.gper.spring.v3.annotation.JZAutowired;
import com.jzhao.gper.spring.v3.annotation.JZController;
import com.jzhao.gper.spring.v3.annotation.JZRequestMapping;
import com.jzhao.gper.spring.v3.annotation.JZRequestParam;
import com.jzhao.gper.spring.v3.webmvc.servlet.JZModelAndView;

/**
 * 公布接口url
 * @author Tom
 *
 */
@JZController
@JZRequestMapping("/web")
public class MyAction {

	@JZAutowired
	IQueryService queryService;
	@JZAutowired
	IModifyService modifyService;

	@JZRequestMapping("/query.json")
	public JZModelAndView query(HttpServletRequest request, HttpServletResponse response,
								@JZRequestParam("name") String name){
		String result = null;
		try {
			result = queryService.query(name);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return out(response,result);
	}
	
	@JZRequestMapping("/add*.json")
	public JZModelAndView add(HttpServletRequest request,HttpServletResponse response,
			   @JZRequestParam("name") String name,@JZRequestParam("addr") String addr){
		String result = modifyService.add(name,addr);
		return out(response,result);
	}
	
	@JZRequestMapping("/remove.json")
	public JZModelAndView remove(HttpServletRequest request, HttpServletResponse response,
								 @JZRequestParam("id") Integer id){
		String result = modifyService.remove(id);
		return out(response,result);
	}
	
	@JZRequestMapping("/edit.json")
	public JZModelAndView edit(HttpServletRequest request,HttpServletResponse response,
			@JZRequestParam("id") Integer id,
			@JZRequestParam("name") String name){
		String result = modifyService.edit(id,name);
		return out(response,result);
	}
	
	
	
	private JZModelAndView out(HttpServletResponse resp,String str){
		try {
			resp.getWriter().write(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
